package com.bsoft.gol.hbs.dao;

import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import hcn.base.TenantProducts;
import org.apache.commons.lang3.StringUtils;
import org.atag.dao.util.BaseDaoImpl;
import pcn.vo.web.Page;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by liuxj on 2016/8/1.
 */
public abstract class TenantProductsDAO extends BaseDaoImpl<TenantProducts> {
    public TenantProductsDAO() {
        this.setKeyField("tenantPId");
    }

    @DAOMethod(sql = "from TenantProducts where tenantPCode=:tenantPCode")
    public abstract List<TenantProducts> findProductTenant(@DAOParam("tenantPCode") String tenantPCode);

    public List<Map<String, Object>> getTenantProductsInfo(final String cloudId) {
        Map<String, Object> params = new HashMap<>();
        StringBuffer hsql = new StringBuffer();
        hsql.append("select a.* from base_tenantproducts a where status='1'");
        if (cloudId != null && StringUtils.isNotEmpty(cloudId)) {
            hsql.append(" and a.cloud_id = :cloudId");
            params.put("cloudId", cloudId);
        }
        return super.findListBySql(hsql.toString(), params, Map.class);
    }

    /**
     * 医联体产品信息-根据医联体产品Id明细信息
     *
     * @param tenantPId
     * @return
     */
    @DAOMethod(sql = "from TenantProducts where tenantPId=:tenantPId and status='1'")
    public abstract TenantProducts getTenantProductById(@DAOParam("tenantPId") int tenantPId);

    public Map<String, Object> getTenantProductsDetailById(final int tenantPId) {
        Map<String, Object> params = new HashMap<>();
        StringBuffer hsql = new StringBuffer();
        hsql.append("SELECT * from base_tenantproducts where tenant_p_id = :tenantPId and status='1'");
        params.put("tenantPId", tenantPId);

        List<Map<String, Object>> list = super.findListBySql(hsql.toString(), params, Map.class);
        return list.isEmpty() ? new HashMap<>() : list.get(0);
    }

    /**
     * 医联体产品信息-根据医联体产品编码查询（新增的判断）
     *
     * @param tenantPCode
     * @return
     */
    @DAOMethod(sql = "from TenantProducts where tenantPCode =:tenantPCode and status='1'")
    public abstract TenantProducts getTenantProductsByCodeForInsert(@DAOParam("tenantPCode") String tenantPCode);

    /**
     * 医联体产品信息-根据医联体产品编码查询（修改的判断）
     *
     * @param tenantPCode
     * @param tenantPId
     * @return
     */
    @DAOMethod(sql = "from TenantProducts where tenantPCode =:tenantPCode and tenantPId<>:tenantPId and status='1'")
    public abstract TenantProducts getTenantProductsByCodeForUpdate(@DAOParam("tenantPCode") String tenantPCode,
                                                                    @DAOParam("tenantPId") int tenantPId);

    /**
     * 逻辑删除
     *
     * @param tenantPId
     */
    @DAOMethod(sql = "update TenantProducts set status='0',lastModify=Now() where tenantPId=:tenantPId")
    public abstract void delete(@DAOParam("tenantPId") int tenantPId);

    /**
     * 获取产品列表 web 分页
     * @param page
     * @return
     */
    public List<Map<String, Object>> queryTenantProductsList(final Page page) {
        Map<String, Object> params = new HashMap<>();

        StringBuffer hsql = new StringBuffer();
        hsql.append("select a.* from base_tenantproducts a where status='1'");
        if (page.getCloudId() != null && StringUtils.isNotEmpty(page.getCloudId())) {
            hsql.append(" and a.cloud_id = :cloudId");
            params.put("cloudId", page.getCloudId());
        }
        return super.findListBySql(hsql.toString(), params, Map.class);
    }


    /**
     * 查询所有产品条数
     *
     * @return
     */
    public Long queryTenantProductsListCount(final String cloudId) {
        Map<String, Object> params = new HashMap<>();
        StringBuffer hsql = new StringBuffer();
        hsql.append("select count(1) from base_tenantproducts a where status='1'");
        if (cloudId != null && StringUtils.isNotEmpty(cloudId)) {
            hsql.append(" and a.cloud_id = :cloudId");
            params.put("cloudId", cloudId);

        }
        return super.findForLongBySql(hsql.toString(), params);
    }

}
